<?php
/**
 * LICENSE
 * 
 * [license information]
 * 
 * @category   Nine
 * @copyright  Copyright (c) 2009 visualidea.org
 * @license    http://license.visualidea.org
 * @version    v 1.0 2009-04-15
 */
require_once 'Nine/Model.php';
require_once 'modules/content/models/ContentCategory.php';
class Models_Content extends Nine_Model
{ 
    /**
     * The primary key column or columns.
     * A compound key should be declared as an array.
     * You may declare a single-column primary key
     * as a string.
     *
     * @var mixed
     */
    protected $_primary = 'content_id';
    /**
     * The field name what we use to group all language rows together
     * 
     * @var string
     */
    protected $_groupField = 'content_gid';
    /**
     * Let system know this is miltilingual table or not.
     * If this table has multilingual fields, Zend_Db_Table_Select object
     * will be inserted language condition automatically.
     * 
     * @var array
     */
    protected $_multilingualFields = array('title', 'alias', 'intro_text', 'full_text', 'hit', 
                                           'last_view_date', 'meta_keywords', 'meta_description', 'enabled');
    
    /**
     * Constructor.
     *
     * Supported params for $config are:
     * - db              = user-supplied instance of database connector,
     *                     or key name of registry instance.
     * - name            = table name.
     * - primary         = string or array of primary key(s).
     * - rowClass        = row class name.
     * - rowsetClass     = rowset class name.
     * - referenceMap    = array structure to declare relationship
     *                     to parent tables.
     * - dependentTables = array of child tables.
     * - metadataCache   = cache for information from adapter describeTable().
     *
     * @param  mixed $config Array of user-specified config options, or just the Db Adapter.
     * @return void
     */
    public function __construct($config = array())
    {
        $this->_name = $this->_prefix . 'content';
        return parent::__construct($config); 
    }

    /**
     * Get all content with conditions
     * 
     * @param $condition
     * @param $order
     * @param $count
     * @param $offset
     */
//    public function getAllContentsWithDefaultLang($condition = array(), $order = null, $count = null, $offset = null)
//    {
//        $select = $this->select()
//                ->setIntegrityCheck(false)
//                ->from(array('s' => $this->_name), array('content_id', 'senabled' => 'enabled', 'publish_up_date' => 'publish_up_date', 'publish_down_date' => 'publish_down_date' , 'ssorting' => 'sorting', 'created_date' => 'created_date'))
//                ->join(array('sl' => $this->_prefix . 'content_lang'), 's.content_id = sl.content_id')
//                ->join(array('sc' => $this->_prefix . 'content_category'), 's.content_category_gid = sc.content_category_gid', array('cname' => 'name'))
//                ->order($order)
//                ->limit($count, $offset);
//        /**
//         * Conditions
//         */
//        if (null != @$condition['keyword']) {
//            $select->where($this->getAdapter()->quoteInto('sl.title LIKE ?', "%{$condition['keyword']}%"));
//        }
//        if (null != @$condition['content_category_gid']) {
//            $select->where("s.content_category_gid = ?", $condition['content_category_gid']);
//        }
//        if (null != @$condition['lang_id']) {
//            $select->where("sl.lang_id = ?", $condition['lang_id']);
//        }
//        
//        return $this->fetchAll($select)->toArray();
//    }

    /**
     * Get all content with conditions
     * 
     * @param $condition
     * @param $order
     * @param $count
     * @param $offset
     */
    public function getAllContent($condition = array(), $order = null, $count = null, $offset = null)
    {
    	$select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('c' => $this->_name))
                ->join(array('cc' => $this->_prefix . 'content_category'), 'c.content_category_gid = cc.content_category_gid', array('cname' => 'name','content_deleteable' => 'content_deleteable'))
                ->join(array('l' => $this->_prefix . 'lang'), 'c.lang_id = l.lang_id', array('lname' => 'name', 'lang_image'))
                ->where('cc.lang_id=?', Nine_Language::getCurrentLangId())
                ->where('c.lang_id=?', Nine_Language::getCurrentLangId())
                ->order($order)
                ->limit($count, $offset);
        /**
         * Conditions
         */
        if (null != @$condition['keyword']) {
            $sql = "SELECT content_gid FROM {$this->_name} WHERE " . $this->getAdapter()->quoteInto('title LIKE ?', "%{$condition['keyword']}%");
            $ids = $this->_db->fetchAll($sql);
            if (empty($ids)) {
                return array();
            }
            $idString = '';
            foreach ($ids as $row) {
                $idString .= $row['content_gid'] . ',';
            }
            /**
             * Add to select object
             */
            $select->where('c.content_gid IN (' . trim($idString, ',') .')');
        }
        if (null != @$condition['content_category_gid']) {
        	
        	$sql = "SELECT gid_string FROM {$this->_prefix}" . 'content_category' . " WHERE " . $this->getAdapter()->quoteInto('content_category_gid = ? ', $condition['content_category_gid']);
        	$ids = @reset($this->_db->fetchAll($sql));
        	if (null == $ids) {
        	    $ids = '0';
        	}
            /**
             * Add to select object
             */
            $select->where('c.content_category_gid IN (' . trim($ids['gid_string'], ',') .')');
        	
        }
        
        if(null == @$condition['pid']) {
        	if (!empty($condition['newProductContentId'])) {
        		$idString = '0,' . implode(',', $condition['newProductContentId']) . ',0';
         		$select->where('c.content_gid IN (' . trim($idString, ',') .')');
        	} 
        }
        else {
        	$tmp = array();
        	$objProperty = new Models_ProductProperty();
        	$allPContents = $objProperty->getAllPropertyByProductGid($condition['pid']);
        	
        	foreach ($allPContents as $content) {
        		$tmp[] = $content['content_gid'];
        	}
        	if (!empty($condition['newProductContentId'])) {
        		$tmp = array_merge($tmp,$condition['newProductContentId']);
        	}
        	$idString = '0,' . implode(',', $tmp) . ',0';
        		
         	$select->where('c.content_gid IN (' . trim($idString, ',') .')');
        }
        
        
        return $this->fetchAll($select)->toArray();
    }
    
	public function getAllEnabledContentByCategory( $catGid, $condition = array(), $order = null, $count = null, $offset = null)
    {
    	
    	$objCat = new Models_ContentCategory();
    	
    	$cat = @reset($objCat->getByColumns(array('content_category_gid=?' => $catGid))->toArray());
    	
    	$gidStr = @trim($cat['gid_string'].',0',','); 
    	/**
    	 * Get all enabled categories
    	 */
    	$select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('cc' => $this->_prefix . 'content_category'), array('content_category_gid'))
                ->where('cc.enabled = 1 AND cc.genabled = 1 AND cc.parent_genabled = 1')
                ->where('cc.content_category_gid IN (' . $gidStr .')');
                
        $cats   = $this->fetchAll($select)->toArray();
        $gidStr = '';
        foreach ($cats as $cat) {
        	$gidStr .= $cat['content_category_gid'] . ',';
        }
        $gidStr = @trim($gidStr.'0',',');
    	
        /**
         * Get all enabled contents in enabled categories
         */
    	$select = $this->select()
                ->where('enabled = 1 AND genabled = 1')
                ->where('content_category_gid IN (' . $gidStr .')')
                ->order($order)
                ->limit($count, $offset);

         /**
          * Condition
          */       
         if (null != @$condition['exclude_content_gids']) {
         	$gidStr = trim($condition['exclude_content_gids'].',0',',');
         	$select->where('content_gid NOT IN (' . $gidStr .')');
         }
               
    	return $this->fetchAll($select)->toArray();
    }
    //add 
 	public function getContentById( $gid ) 
    {
    	$select = $this->select()
    			->where('content_gid=?',$gid)
                ->where('lang_id=?', Nine_Language::getCurrentLangId());
    	
    	return @reset($this->fetchAll($select)->toArray());
    }
    // end add
    
    public function getLatestContentByCategory( $catGid )
    {
    	$allContent = $this->getAllEnabledContentByCategory($catGid, array(), array('sorting ASC','content_gid DESC','content_id DESC'),1,0);
    	return @reset($allContent);
    }
    
    public function increaseSorting($startPos = 1, $num = 1)
    {
        $sql = "UPDATE {$this->_name} SET sorting = sorting + " . intval($num) . " WHERE sorting >= " . intval($startPos);
        
        $this->_db->query($sql);
    }
    
    /**
     * Get content by gid
     * 
     * @param int $gid
     * @return Zend_Db_Table_Row
     */
    public function getContentByGid($gid)
    {
        $this->setAllLanguages(false);
        $select = $this->select()
                ->where('content_gid=?', $gid);
                
        return $this->fetchRow($select);
    }
//    public function getContentByUrl($url, $langId)
//    {
//        $url = $this->getAdapter()->quote($url);
//        $langId = intval($langId);
//        $time = time();
//        
//        $query = "
//                 SELECT *
//                 FROM ( SELECT * 
//                        FROM {$this->_prefix}content_lang 
//                        WHERE lang_id = {$langId} AND enabled = 1 AND url = {$url} 
//                       ) AS sl
//                 JOIN ( SELECT content_id, content_category_gid, enabled AS senabled, publish_up_date, publish_down_date, sorting AS ssorting, created_date, layout,image
//                        FROM {$this->_prefix}content 
//                        WHERE enabled = 1 AND publish_up_date <= {$time} AND (publish_down_date = 0 OR publish_down_date > {$time} )
//                       ) AS s
//                 ON s.content_id = sl.content_id
//                 LIMIT 0,1
//        ";
////        echo $query;die;
//        $result =$this->_db->fetchRow($query);
//         $array = explode(" ", $result['title']);
//         
//        foreach ($array as $item){
//           $result['titleeach'][] = substr($item,0,1); 
//           $result['titleeach'][] = substr($item,1,strlen($item)-1); 
//        }
//        return $result;
//    }

	public function checkExistingAlias($alias, $exclude = array()) {
    	$select = $this->select()
    			->where('alias = ?', $alias);

    	if (!empty($exclude)) {
    		$excludeQuery = '0';
    		foreach ($exclude as $id) {
    			$excludeQuery .= $this->getAdapter()->quoteInto(' OR content_gid != ?', $id);
    		}
    		$select->where($excludeQuery);
    	}
    	$result = $this->fetchRow($select);
    	
    	if (null == $result) {
    		return false;
    	}
    	else {
    		return true;
    	}
    }
    
    public function getNextContentId()
    {
    	$select = $this->select()
    			->from(array('c' => $this->_name), array('content_id'))
    			->order('content_id DESC')
    			->limit(1,0);
    			
    	return (int)$this->getAdapter()->fetchOne($select) + 1;
    }
}